#include "wgraph.h"
#include <QPainter>

const int FIX_X = 50;
const int FIX_Y = 50;
const int DIST_X = 10;
const int DIST_LABEL = 40;
const int DIST_Y = 10;
const int MAX_X = 1024;
const int MAX_Y = 720;

wGraph::wGraph(QWidget *parent, GData _data) :
    QWidget(parent), data(_data)
{
    this->resize(MAX_X, MAX_Y);
    this->setFixedSize(MAX_X+140,MAX_Y+10);
}

void wGraph::paintEvent(QPaintEvent *event){

    QPainterPath axis, path, nodes, solved, label;
    QFont font;

    //cpu line
    int orig_x = FIX_X;
    int long_y = FIX_Y;

    //diff : length = inc : dist
    int long_x = MAX_X - FIX_X;
    int orig_y = MAX_Y - FIX_Y;

    int nInc_x = (int)data.cpuTime.size()-1;
    float inc_x = (float)(long_x - orig_x)/nInc_x;

    //too small
    if (inc_x < DIST_X) {
        inc_x = DIST_X;
        nInc_x = (long_x - orig_x)/DIST_X;
    }

    //x line
    axis.moveTo(orig_x,orig_y);
    axis.lineTo(long_x,orig_y);
    int x;
    for (int i=0; i<nInc_x+1; i++){
        x = orig_x + (int)(i*inc_x);
        axis.moveTo(x, orig_y - 5);
        axis.lineTo(x, orig_y + 5);
    }

    int nInc_y = (orig_y - long_y)/DIST_Y;
    int inc_y = DIST_Y;

    //y line
    axis.moveTo(orig_x,orig_y);
    axis.lineTo(orig_x,long_y);
    int y; float lastIntY=-1;
    for (int i=0; i<nInc_y+1; i++){
        y = orig_y - (int)(i*inc_y);
        axis.moveTo(orig_x - 5, y);
        axis.lineTo(orig_x + 5, y);
        // y : l_y = x : l_x
        float l = (i*inc_y)*(data.maxY)/(orig_y - long_y);
        if(i%5 == 0&&(l!=lastIntY)){
            label.addText(orig_x-DIST_LABEL, orig_y - (int)(i*inc_y),font, QString::number(l));
            lastIntY = l;
        }
    }

    //data
    //check per evitare divisioni per 0
    if (data.maxY - data.minY == 0) data.maxY = data.minY + 1;
    int tmp = 1,lastlab = orig_x;
    y = orig_y - data.cpuTime[0]*(orig_y - long_y)/data.maxY;
    path.moveTo(orig_x, y);
    nodes.addEllipse(QRectF(orig_x,y,2,2));
    y = orig_y - data.solved[0]*(orig_y - long_y)/data.nIst;
    solved.moveTo(orig_x,y);
    label.addText(orig_x, orig_y+30,font, QString::number(data.minX));
    for (unsigned int i=1; i<data.cpuTime.size(); i++){
        if((int)data.cpuTime.size() > nInc_x){
            if(i%(data.cpuTime.size()/nInc_x) == 0){
                x = orig_x + (int)(tmp*inc_x);
                y = orig_y - data.cpuTime[i]*(orig_y - long_y)/data.maxY;
                tmp++;
                path.lineTo(x, y);
                nodes.addEllipse(QRectF(x-2,y-2,4,4));
                y = orig_y - data.solved[i]*(orig_y - long_y)/data.nIst;
                solved.lineTo(x,y);
                if (lastlab + DIST_LABEL < x){
                    label.addText(x, orig_y+30,font, QString::number(data.minX+i*data.inc));
                    lastlab = x;
                }
            }
        }
        else{
            x = orig_x + (int)(i*inc_x);
            y = orig_y - data.cpuTime[i]*(orig_y - long_y)/data.maxY;
            path.lineTo(x, y);
            nodes.addEllipse(QRectF(x-2,y-2,4,4));
            y = orig_y - data.solved[i]*(orig_y - long_y)/data.nIst;
            solved.lineTo(x,y);
            if (lastlab + DIST_LABEL < x){
                label.addText(x, orig_y+30,font, QString::number(data.minX+i*data.inc));
                lastlab = x;
            }
        }
    }


    //label
    label.addText(orig_x - 10, long_y-10,font, tr("CPU TIME (ms)"));
    label.addText((long_x + orig_x)/2, orig_y + 50,font, data.mode);

    //legenda
    x = MAX_X-35;
    axis.addText(x+30, long_y,font,tr("Legenda:"));
    axis.addText(x+30, long_y+DIST_LABEL/2,font,tr("CPU time"));
    axis.addText(x+30, long_y+DIST_LABEL,font,tr("andamento percentuale"));
    axis.addText(x+30, long_y+DIST_LABEL+15,font,tr("problemi risolti"));

    path.moveTo(x,long_y+DIST_LABEL/2 -5);
    path.lineTo(x+20,long_y+DIST_LABEL/2 -5);
    solved.moveTo(x, long_y+DIST_LABEL);
    solved.lineTo(x+20,long_y+DIST_LABEL);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.drawPath(axis);
    painter.drawPath(label);
    painter.setPen(Qt::blue);
    painter.drawPath(path);
    painter.setPen(Qt::red);
    painter.drawPath(nodes);
    painter.setPen(Qt::green);
    painter.drawPath(solved);

}
